package com.chw.springbootmybatis.interceptor;

import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.util.List;

@Intercepts({@Signature(
        type = StatementHandler.class,
        method = "prepare", args = {Connection.class, Integer.class}
)})
@Component
public class SqlInterceptor implements Interceptor {


    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        RoutingStatementHandler routingStatementHandler =  (RoutingStatementHandler) invocation.getTarget();
        String sql = routingStatementHandler.getBoundSql().getSql();
        Object parameterObject = ((RoutingStatementHandler) invocation.getTarget()).getBoundSql().getParameterObject();
        String replace = sql.replace("?", parameterObject.toString());
        System.out.println(replace);
        return invocation.proceed();
    }
}
